package com.taobao.machinesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.SysUserRole;
import com.taobao.machinesystem.mapper.SysUserMapper;
import com.taobao.machinesystem.service.SysUserRoleService;
import com.taobao.machinesystem.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taobao.machinesystem.utilis.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 根据账号查询用户
     * @param username
     * @return
     */
    @Override
    public SysUser queryUserByLogin(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        return sysUser;
    }

    /**
     * 新增一个用户
     *
     * @param user
     * @return
     */
    @Override
    public int insertSysUser(SysUser user) {
        //获取到用户信息，对密码进行MD5加密
        user.setPassword(MD5Utils.changePasswordMD5(user.getPassword()));
        int insert = this.sysUserMapper.insert(user);
        return insert;
    }

    /**
     * 根据ID修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public int updateByIdSysUser(SysUser user) {
        int i = this.sysUserMapper.updateById(user);
        return i;
    }

    /**
     * 分页查询用户
     *
     * @param currentPage
     * @param size
     * @param username
     * @return
     */
    @Override
    public Page<SysUser> listSysUser(Integer currentPage, Integer size, String username) {
        Page<SysUser> sysUserPage = new Page<SysUser>(currentPage, size);

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(username)) {
            queryWrapper.like("username", username);
        }

        List<SysUser> records = sysUserPage.getRecords();
        log.info(records.toString());

        return baseMapper.selectPage(sysUserPage, queryWrapper);
    }

    /**
     * 根据id查询单条用户信息
     *
     * @param id·
     * @return
     */
    @Override
    public SysUser selectByIdSysUser(String id) {
        SysUser sysUser = this.sysUserMapper.selectById(id);
        return sysUser;
    }

    /**
     * 根据id修改用户信息
     *
     * @param user
     * @param newPassword
     * @return
     */
    @Override
    public Integer updateSysUser(SysUser user, String newPassword) {
        //先查询出来
        SysUser sysUser = this.selectByIdSysUser(user.getId());
        //赋值
        sysUser.setNickName(user.getNickName());
        sysUser.setToken(user.getToken());
        Integer integer = 0;
        //假如用户需要修改密码，则先判断密码是否正确
        if (!StringUtils.isEmpty(user.getPassword())) {
            //按照id和密码查询，判断是否有这个用户
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", sysUser.getId());
            queryWrapper.eq("password", MD5Utils.changePasswordMD5(user.getPassword()));
            integer = this.sysUserMapper.selectCount(queryWrapper);
            System.out.println(integer == 0);
            if (integer == 0) {
                return 0;
            }
            //如果用户正确，则将新密码加密赋值进去。
            sysUser.setPassword(MD5Utils.changePasswordMD5(newPassword));
        }
        //再进行修改用户
        int i = this.updateByIdSysUser(sysUser);
        return i;
    }

    /**
     * 逻辑删除用户
     *
     * @param id
     * @return
     */
    @Override
    public Integer isDeleteSysUser(String id) {
        int i = this.sysUserMapper.deleteById(id);
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createUserRole(String userId, String[] roleIds) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        for (String roleId : roleIds) {
            sysUserRole.setRoleId(roleId);
            sysUserRoleService.save(sysUserRole);
        }
        return true;
    }

}
